﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Chess.Localization;
using Chess.ScreensManager;

namespace Chess.Screens
{
    /// <summary>
    /// A popup message box screen, used to choose figure
    /// for pawn transformation.
    /// </summary>
    class ChooseFigureBoxScreen : GameScreen
    {
        #region Fields

        SpriteFont font;

        //Rectangle backgroundRectangle;
        Vector2 messagePosition;
        Vector2 queenPosition;
        Vector2 rookPosition;
        Vector2 bishopPosition;
        Vector2 knightPosition;

        Rectangle queenRectangle;
        Rectangle rookRectangle;
        Rectangle bishopRectangle;
        Rectangle knightRectangle;

        string message = Strings.choosefigurebox_message;
        string queen = Strings.choosefigurebox_queen;
        string rook = Strings.choosefigurebox_rook;
        string bishop = Strings.choosefigurebox_bishop;
        string knight = Strings.choosefigurebox_knight;

        Texture2D gradientTexture;

        public event EventHandler<FigureEventArgs> FigureChoosed;

        #endregion

        #region Initialization

        public ChooseFigureBoxScreen()
        {
            IsPopup = true;

            TransitionOnTime = TimeSpan.FromSeconds(0.2);
            TransitionOffTime = TimeSpan.FromSeconds(0.2);
        }


        /// <summary>
        /// Loads graphics content for this screen. This uses the shared ContentManager
        /// provided by the Game class, so the content will remain loaded forever.
        /// Whenever a subsequent MessageBoxScreen tries to load this same content,
        /// it will just get back another reference to the already loaded data.
        /// </summary>
        public override void LoadContent()
        {
            ContentManager content = ScreenManager.Game.Content;

            gradientTexture = content.Load<Texture2D>(@"Menu\gradient");

            font = ScreenManager.Font;

            // Center the message text in the viewport.
            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;

            Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);

            Vector2 messageSize = font.MeasureString(message);
            Vector2 queenSize = font.MeasureString(queen);
            Vector2 rookSize = font.MeasureString(rook);
            Vector2 bishopSize = font.MeasureString(bishop);
            Vector2 knightSize = font.MeasureString(knight);

            messagePosition = (viewportSize - new Vector2(messageSize.X, 
                messageSize.Y + queenSize.Y + rookSize.Y + bishopSize.Y + knightSize.Y)) / 2.0f;
            queenPosition = new Vector2((viewport.Width - queenSize.X) / 2.0f, messagePosition.Y + messageSize.Y);
            rookPosition = new Vector2((viewport.Width - rookSize.X) / 2.0f,
                messagePosition.Y + messageSize.Y + queenSize.Y);
            bishopPosition = new Vector2((viewport.Width - bishopSize.X) / 2.0f,
                messagePosition.Y + messageSize.Y + queenSize.Y + rookSize.Y);
            knightPosition = new Vector2((viewport.Width - knightSize.X) / 2.0f,
                messagePosition.Y + messageSize.Y + queenSize.Y + rookSize.Y + bishopSize.Y);

            queenRectangle = new Rectangle(
                (int)queenPosition.X, (int)queenPosition.Y, (int)queenSize.X, (int)queenSize.Y);
            rookRectangle = new Rectangle(
                (int)rookPosition.X, (int)rookPosition.Y, (int)rookSize.X, (int)rookSize.Y);
            bishopRectangle = new Rectangle(
                (int)bishopPosition.X, (int)bishopPosition.Y, (int)bishopSize.X, (int)bishopSize.Y);
            knightRectangle = new Rectangle(
                (int)knightPosition.X, (int)knightPosition.Y, (int)knightSize.X, (int)knightSize.Y);

            // The background includes a border somewhat larger than the text itself.
            //const int hPad = 32;
            //const int vPad = 16;
            //int rectangleX = (int)Math.Min(messagePosition.X, Math.Min(queenPosition.X, Math.Min(rookPosition.X,
            //    Math.Min(bishopPosition.X, knightPosition.X))));
            //int rectangleY = (int)Math.Min(messagePosition.Y, Math.Min(queenPosition.Y, Math.Min(rookPosition.Y,
            //    Math.Min(bishopPosition.Y, knightPosition.Y))));
            //int rectangleWidth = (int)Math.Max(messageSize.X, Math.Min(queenSize.X, Math.Min(rookSize.X,
            //    Math.Min(bishopSize.X, knightSize.X))));
            //int rectangleHeight = (int)(messageSize.Y + queenSize.Y + rookSize.Y + bishopSize.Y + knightSize.Y);
            //backgroundRectangle = new Rectangle(rectangleX - hPad, rectangleY - vPad,
            //    rectangleWidth + hPad * 2, rectangleHeight + vPad * 2);
        }

        #endregion

        #region Handle Input

        /// <summary>
        /// Responds to user input, accepting or cancelling the message box.
        /// </summary>
        public override void HandleInput(InputState input)
        {
            if (input.IsNewKeyPress(Keys.Q))
            {
                if (FigureChoosed != null)
                    FigureChoosed(this, new FigureEventArgs(FigureTypes.Queen));

                ExitScreen();
            }
            else if (input.IsNewKeyPress(Keys.R))
            {
                if (FigureChoosed != null)
                    FigureChoosed(this, new FigureEventArgs(FigureTypes.Rook));

                ExitScreen();
            }
            else if (input.IsNewKeyPress(Keys.B))
            {
                if (FigureChoosed != null)
                    FigureChoosed(this, new FigureEventArgs(FigureTypes.Bishop));

                ExitScreen();
            }
            else if (input.IsNewKeyPress(Keys.K))
            {
                if (FigureChoosed != null)
                    FigureChoosed(this, new FigureEventArgs(FigureTypes.Knight));

                ExitScreen();
            }

            if (input.IsLeftButtonPressed())
            {
                if (queenRectangle.Contains(new Point(input.CurrentMouseState.X, input.CurrentMouseState.Y)))
                {
                    if (FigureChoosed != null)
                        FigureChoosed(this, new FigureEventArgs(FigureTypes.Queen));

                    ExitScreen();
                }
                else if (rookRectangle.Contains(new Point(input.CurrentMouseState.X, input.CurrentMouseState.Y)))
                {
                    if (FigureChoosed != null)
                        FigureChoosed(this, new FigureEventArgs(FigureTypes.Rook));

                    ExitScreen();
                }
                else if (bishopRectangle.Contains(new Point(input.CurrentMouseState.X, input.CurrentMouseState.Y)))
                {
                    if (FigureChoosed != null)
                        FigureChoosed(this, new FigureEventArgs(FigureTypes.Bishop));

                    ExitScreen();
                }
                else if (knightRectangle.Contains(new Point(input.CurrentMouseState.X, input.CurrentMouseState.Y)))
                {
                    if (FigureChoosed != null)
                        FigureChoosed(this, new FigureEventArgs(FigureTypes.Knight));

                    ExitScreen();
                }
            }
        }

        #endregion

        #region Draw

        /// <summary>
        /// Draws the message box.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

            // Darken down any other screens that were drawn beneath the popup.
            ScreenManager.FadeBackBufferToBlack(TransitionAlpha * 2 / 3);

            // Fade the popup alpha during transitions.
            Color color = new Color(255, 255, 255, TransitionAlpha);

            spriteBatch.Begin();

            // Draw the background rectangle.
            //spriteBatch.Draw(gradientTexture, backgroundRectangle, color);

            // Draw the message box text.
            spriteBatch.DrawString(font, message, messagePosition, color);
            spriteBatch.DrawString(font, queen, queenPosition, color);
            spriteBatch.DrawString(font, rook, rookPosition, color);
            spriteBatch.DrawString(font, bishop, bishopPosition, color);
            spriteBatch.DrawString(font, knight, knightPosition, color);

            spriteBatch.End();
        }

        #endregion
    }
}